<?php 
	//timer pengecekan load page...
	include("timer.php");
	$_start_timer = get_microtime();

	require_once("protect1.php"); 
	require_once("connect.php");

	// $pilih = mysql_select_db("uwika_krs",$conn);
	$pdo->query("USE uwika_krs");
?>
<STYLE type=text/css>.blank {
	FONT-SIZE: 0px; WIDTH: 0px; HEIGHT: 0px
}
TD {
	FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
}
INPUT {
	FONT-SIZE: 12px; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
}
.present {
	FONT-WEIGHT: bold; BACKGROUND-COLOR: #dee5ec
}
.h1 {
	FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #ffffff
}
.d1 {
	COLOR: #c0c0c0
}
.link {
	FONT-WEIGHT: bold; FONT-SIZE: 12px; CURSOR: hand; COLOR: #ffffff; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
.footer {
	FONT-WEIGHT: normal; FONT-SIZE: 10px; COLOR: #000000; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif; TEXT-DECORATION: none
}
.footer1 {
	FONT-WEIGHT: bold; FONT-SIZE: 10px; BACKGROUND-IMAGE: url(chat/images/menu_b.gif); CURSOR: default; COLOR: #000000; BORDER-BOTTOM: 0px
}
.header {
	FONT-WEIGHT: bold; FONT-SIZE: 10px; BACKGROUND-IMAGE: url(chat/images/menu_h.gif); CURSOR: default; COLOR: #000000; BORDER-BOTTOM: 0px
}
.textcol {
	FONT-SIZE: 12px; COLOR: #2378d5; FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif
}
body {
	background-color: #FFFFFF;
	background-image: url(bg.jpg);
	background-repeat: repeat;
}
a:link {
	color: #FFFFFF;
	text-decoration: none;
}
a:visited {
	text-decoration: none;
	color: #FFFFFF;
}
a:hover {
	text-decoration: underline;
	color: #021C8A;
}
a:active {
	text-decoration: none;
	color: #001D86;
}
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 11pt;
	color: #FFFFFF;
}
</STYLE>
<title>:: Nilai KHS &amp; IPK ::</title><table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="7e96dc">&nbsp;</td>
  </tr>
  <tr>
    <td><img src="images/bannerAtas.jpg" alt="image" width="780" height="200" /></td>
  </tr>
  <tr>
    <td bgcolor="3a5db1">
	<div align = center>
	<?php
		//menambah periode, tinggal tambahkan array ini
		$_periode = array('1-00-01', '2/00-01'
						 ,'1/01-02', '2/01-02'
						 ,'1/02-03', '2/02-03'
						 ,'1/03-04', '2/03-04'
						 ,'1/04-05', '2/04-05'
						 ,'1/05-06', '2/05-06'
						 ,'1/06-07', '2/06-07'
						 ,'1/07-08', '2/07-08'
						 ,'1/08-09', '2/08-09'
						 ,'1/09-10', '2/09-10'
						 ,'1/10-11', '2/10-11'
						 ,'1/11-12', '2/11-12'
						 ,'1/12-13', '2/12-13'
						 ,'1/13-14', '2/13-14'
						 ,'1/14-15', '2/14-15'
						 ,'1/15-16', '2/15-16');
		$_nrp = $reg_uname;
		$_semester = 1;
		for($i = 0; $i < count($_periode) ; $i++ ){
			$sql="SELECT kodemk,nama,sks,nilai from sks WHERE nrp=:nrp AND periode=:periode";
			$stmt = $pdo->prepare($sql);
			$stmt->execute(array(':nrp' => $_nrp
								,':periode' => $_periode[$i] ));
			if($stmt->rowCount() > 0){
				$table_colour = 0;
				//
				$totsks = 0;
				$totbobot = 0;
				//
				//cetak tabel header
				?>
					SEMESTER : <B><?php echo $_semester; ?></b> <br>
					PERIODE : <b><?php echo $_periode[$i]; ?></b>
					<table width="90%" border="0">
						<tr bgcolor="#001E8C">
							<td width="5%"><b><center>KODE</center></td>
							<td width="20%"><B><center>MATA  KULIAH</center></td>
							<td width="5%"><B><center>SKS</center></td>
							<td width="3%"><b><center>GRADE</center></td>
						</tr>
				<?php
				while($_data = $stmt->fetch(PDO::FETCH_OBJ)){ //fetch data to stdclass object
					//cetak isi
					?>
						<tr bgcolor="<?php if(($table_colour % 2) == 0){ echo "#4585cd"; } else { echo "#7e96dc"; } $table_colour++; ?>">
							<td><center><?php echo $_data->kodemk ?></center></td>
							<td><?php echo $_data->nama; ?></td>
							<td><center><?php echo $_data->sks ?></center></td>
							<td><center>
							<?php 
								if ($_data->nilai != 'BL') {
									$totsks = $totsks + $_data->sks;
									$totbobot = $totbobot + (huruf2angka($_data->nilai) * $_data->sks);
								}
								echo $_data->nilai;
							?>
							</center></td>
						</tr>
					<?php
					//cetak finish
				}

				$_semester++;
				//memperbaiki error division by zero saat totalSKS = 0, karna nilai mk belum lulus semua.
				?>
					</table>
					<div align = center>
						Total SKS : <b><?php echo $totsks; ?></b> | Nilai IPS : <b><?php if($totsks > 0){ echo round($totbobot / $totsks,2); } else { echo 0; } ?></b> 
					</div><br><br>
				<?php
			}
		}
		?>
			<br> <div align = center>
			<font face="Arial" size=4><b>
		<?php

		$allnilai = 0;
		$allsks = 0;
		$sql = "SELECT kodemk, sks, nilai FROM `sks` WHERE nrp=:nrp Order by kodemk, nilai";
		
		// $sql = "SELECT distinct(kodemk),sks, nilai FROM `sks` WHERE nrp=:nrp"; 
		$stmt = $pdo->prepare($sql);
		$stmt->execute(array(':nrp' => $_nrp));
		$_data = $stmt->fetchAll(PDO::FETCH_OBJ);
		
		/*
		for($i = 0; $i <count($_data); $i++){
			print_r($_data[$i]);
			echo "<br \>";
		}
		echo "<br \><br \>";
		*/
		
		//hapus data ganda! dan pakai nilai tertinggi
		//ini bisa digunakan ketika nantinya sks dipakai sendiri. tetapi rasanya tidak... ~.~
		for($i = 0; $i <count($_data); $i++){
			for($j = 1; $j <count($_data) - 1; $j++){
				if( ($_data[$i]->kodemk == $_data[$j]->kodemk) && ($i!=$j) && (isset($_data[$i])) && (isset($_data[$j])) ){
					if(huruf2angka($_data[$i]->nilai) >= huruf2angka($_data[$j]->nilai)){
						$_data[$j] = null;
					}else{
						$_data[$i] = null;
					}
				}
			}
		}
		
		/*
		for($i = 0; $i <count($_data); $i++){
			if(isset($_data[$i])){
				print_r($_data[$i]);
				echo "<br \>";
			}
		}
		*/
		
		
		//hitung nilai yg bukan bukan BL. setelah data dihapus yg ganda dan diambil nilai tertinggi
		foreach($_data as $_datafor){ //fetch data to stdclass object
			if(isset($_datafor)){
				if($_datafor->nilai != "BL"){
					$allsks = $allsks + $_datafor->sks;
					$allnilai = $allnilai + (huruf2angka($_datafor->nilai) * $_datafor->sks);
				}
			}
		}
		?>
			Total SKS yang sudah anda tempuh sampai saat ini : <font size=6 color = black><u><?php echo $allsks; ?></u></font>
			<br>
			Nilai IPK Anda saat ini :
			<u><font size=6 color = black><?php if($allsks > 0){ echo round($allnilai / $allsks,3); } else { echo 0; } ?></u></font>
			</font></b>
			<br>
			</div><br>
		<?php
		
		include("footer.php");

		/*
		 * penghitungan SKS yg lama, yg menghitung semua sks yg BL serta mengambil nilai acak (tidak up2date)
		$allnilai = 0;
		$allsks = 0;
		
		$sql = "SELECT distinct(kodemk),sks, nilai FROM `sks` WHERE nrp=:nrp"; 
		$stmt = $pdo->prepare($sql);
		$stmt->execute(array(':nrp' => $_nrp));
		// echo "<script>alert('$stmt->rowCount()')</script>";
		while($_data = $stmt->fetch(PDO::FETCH_OBJ)){ //fetch data to stdclass object
			$allsks = $allsks + $_data->sks;
			$allnilai = $allnilai + (huruf2angka($_data->nilai) * $_data->sks);
		}
		?>
			Total SKS yang sudah anda tempuh sampai saat ini : <font size=6 color = black><u><?php echo $allsks; ?></u></font>
			<br>
			Nilai IPK Anda saat ini :
			<u><font size=6 color = black><?php echo round($allnilai / $allsks,3); ?></u></font>
			</font></b>
			<br>
			</div><br>
		<?php
		
		*/

	/*
	$nrp=$reg_uname;
	$allsks=0;
	$allnilai=0;
	$smt=0; // ditambah tuk melihat KHS & KRS yang lampau
	tampil('1/00-01',$nrp,$smt);
	tampil('2/00-01',$nrp,$smt);
	tampil('1/01-02',$nrp,$smt);
	tampil('2/01-02',$nrp,$smt);
	tampil('1/02-03',$nrp,$smt);
	tampil('2/02-03',$nrp,$smt);
	tampil('1/03-04',$nrp,$smt);
	tampil('2/03-04',$nrp,$smt);
	tampil('1/04-05',$nrp,$smt);
	tampil('2/04-05',$nrp,$smt);
	tampil('1/05-06',$nrp,$smt);
	tampil('2/05-06',$nrp,$smt);
	tampil('1/06-07',$nrp,$smt);
	tampil('2/06-07',$nrp,$smt);
	tampil('1/07-08',$nrp,$smt);
	tampil('2/07-08',$nrp,$smt);
    tampil('1/08-09',$nrp,$smt);
    tampil('2/08-09',$nrp,$smt);
    tampil('1/09-10',$nrp,$smt);
    tampil('2/09-10',$nrp,$smt);
    tampil('1/10-11',$nrp,$smt);
	tampil('2/10-11',$nrp,$smt);
	tampil('1/11-12',$nrp,$smt);
	tampil('2/11-12',$nrp,$smt);
	echo "<br> <div align = center>";
	echo "<font face=\"Arial\" size=4><b>";

	$allsks=0;
	$sqlstr="SELECT distinct(kodemk),sks, nilai FROM `sks` WHERE nrp='$nrp'"; // and nilai <> 'BL'";
	$hasil=mysql_query($sqlstr);
	$row = mysql_fetch_row($hasil);
	do {
		list($kodemk,$sks)=$row;
		$allsks=$allsks + $sks;
		echo ("$nilai");
		$allnilai=$allnilai + (huruf2angka($row[2]) * $sks);
	} while ($row=mysql_fetch_row($hasil));
	echo "Total SKS yang sudah anda tempuh sampai saat ini : <font size=6 color = black><u>$allsks</u></font>";
	echo "<br>";
	echo "Nilai IPK Anda saat ini : ";
	echo "<u><font size=6 color = black>" . round($allnilai / $allsks,3) . "</u></font>";
	echo "</font></b>";
	echo "<br>";

	function tampil($periode,$nrp,$smt) {
		global $smt;
		$sqlstr="select kodemk,nama,sks,nilai from sks where nrp='$nrp' and periode='$periode'";
		$hasil=mysql_query($sqlstr);
		list($kodemk) = mysql_fetch_row($hasil);
		if (!empty($kodemk)) {
			$smt=$smt + 1;
			$totsks=0;
			$totbobot=0;
			$sqlstr="select kodemk,nama,sks,nilai from sks where nrp='$nrp' and periode='$periode'";
			$hasil=mysql_query($sqlstr);
			echo "<div align = center>";
			echo "SEMESTER : <b>$smt</b> <br>";
			echo "PERIODE : <b>$periode</b>";
			echo "</div>";
			echo "<table width=\"70%\" border=\"0\" align = center>";
			echo "<tr bgcolor=\"#001E8C\">";
			echo "<td width=\"5%\"><b><center>K O D E</center></td>";
			echo "<td width=\"20%\"><B><center>N A M A &nbsp; M A T A  &nbsp; K U L I A H</center></td>";
			echo "<td width=\"5%\"><B><center>SKS</center></td>";
			echo "<td width=\"3%\"><b><center>GRADE</center></td>";
			echo "</tr>";
			$row = mysql_fetch_row($hasil);
			$bantuan=0;
			do {
				$bantuan++;
				list($kodemk,$nama,$sks,$nilai) = $row;
				if (($bantuan % 2) == 0) {
				     echo "<tr bgcolor=\"#4585cd\">";
				} else {
					 echo "<tr bgcolor=\"#7e96dc\">";
				}
				echo "<td><center>$kodemk</center></td>";
				echo "<td>$nama</td>";
				echo "<td><center>$sks</center></td>";
				if ($nilai != 'BL') {
					$totsks=$totsks + $sks;
					$totbobot=$totbobot + (huruf2angka($nilai) * $sks);
				}		
				echo "<td><center>$nilai</center></td></tr>";
			} while ($row=mysql_fetch_row($hasil));
			echo "</table>";
			echo ("<div align = center>");
			echo "Total SKS : <b>$totsks</b> &nbsp&nbsp | &nbsp&nbsp ";
			$allsks=$allsks+$totsks;
			echo "Nilai IPS : <b> ";
			echo round($totbobot / $totsks,3);
			echo "</b> </div><br><br>";
		}
	}
*/

	function huruf2angka($nilai) {
		switch ($nilai) {
			case "A" : { return 4; break; }
			case "AB" :{ return 3.5; break; }
			case "B" :{ return 3; break; }
			case "BC" :{ return 2.5; break; }
			case "C" :{ return 2; break; }
			case "D" :{ return 1; break; }
			case "E" :{ return 0; break; }
			default : { return 0; break; }
		}
	}
?>
	&nbsp;</td>
  </tr>
  <tr>
    <td bgcolor="7e96dc"><div align="center">
	<footer id="main">
	  <a href="http://www.widyakartika.ac.id/perpustakaan">Perpustakaan Online</a> | <a href="http://krs.widyakartika.ac.id/">KRS Online</a>
	  <br \>
	  Copyright &copy; 2012. <a href="http://www.widyakartika.ac.id">Universitas Widya Kartika</a> & <a href="#">Labkom ICT</a> - All rights reserved
	  <p>
		<?php
			$_finish_timer = get_microtime();
			echo 'Page generated in '.round(($_finish_timer - $_start_timer), 4).' seconds.';
		?>
	</footer>
	</div></td>
  </tr>
</table>